Skip to main content

Create a Backend Hello World! Script

Create a new backend script#

Backend scripts are used by the Object Model API, Datasources Service, and other Twinit Services. Unlike the client scripts they are vanilla javascript and not ES Modules.

Expand your project in the extension's 'Project's, Scripts & Configs' tree. You will see there are now tree nodes for Scripts, User Configs, and API Configs. If you expand each you can see they are both empty.

  1. Right click on the Scripts node and click 'Create New Backend Script'
  2. In the Create Script form
    1. For Script Name enter '01 - Hello World Backend'
    2. For Script Description enter 'My First Backend Script'
    3. For Script Short Name enter 'hwb'
    4. For Script User Type enter 'hello-back'
    5. Leave the default script content for now, we will update it later
  3. Click the Save button

The Scripts node will then refresh to display the new script you have created.

The cloud icon indicates the script only exists on the Twinit platform, and you do not have it local to edit.

Also notice that the script name is appended with '\<b>' to indicate that it is a backend script.

Open the script#

  1. Right click the '[v1] 01 - Hello World Backend' script you just created, click Open, and it will open in a new window

Notice the cloud icon next to the script changed to a document icon indicating that you now have that script local for editing.

Understanding the script signature#

The script signature for backend scripts is identical to the script signature for client scripts, except the available libraries are different and the callback parameter is not supported.

input - Any and all input to the script. This could be data passed by a client application, data passed in from a previous step in a datasources orchestrator, request data from an OMAPI endpoint, or any other input coming from the caller of the script

libraries - The various libraries provided to the script for interacting with the Twinit platform or the user. Currently these libraries are:

Twinit libraries:

  • PlatformApi
  • IafScriptEngine
  • CoreUtils

3rd Party Libraries

  • fs
  • path
  • unzipper
  • uuidv4
  • _ (lodash)
  • moment
  • dayjs
  • mingo
  • ajv
  • chrono

For documentation on 3rd party libraries please refer to the libraries documentation provided by the maintainers of the library.

ctx - Information such as the user's token, current project, _namespaces, and other contextual information. Every api call to Twinit, which uses any Iaf javascript API, must include a ctx.

Edit the script#

  1. Rename the default 'scriptFunction' to 'helloBack' and update the default export to 'helloWorld'
  2. In the script itself add a return statement:
function helloBack(input, libraries, ctx) {
    return 'Hello World!'
}
  1. In order to run backend scripts using the Twinit IDE Extension we must also provide a getRunnableScripts function exposing the scripts to be run
function getRunnableScripts() {
    return [
        {name: 'Script', script: 'scriptFunction'}
    ]
}
  1. In the getRunnableScripts function change the name of the Script to 'Hello Backend' and the script to 'helloBack'
function helloBack(input, libraries, ctx) {
    return 'Hello World!'
}
function getRunnableScripts() {
    return [
        {name: 'Hello Backend', script: 'helloBack'}
    ]
}
  1. Then save the script locally (Ctrl-S or File > Save)

Notice the document icon next to the script has changed to a triangular alert icon. This indicates that your local copy of the script differs from the version of the script on the Twinit platform, because you have changed it locally.

Run the script#

  1. Right click on the 'B01 - Hello World Backend' script and click 'Run Script'
  2. When asked to select a script to run at the top of the vscode window select 'Hello Backend'

While the script is running you will see a status indicator in the bottom left hand corner of the IDE status bar. When the script has completed running results of the script will display.

Understanding script results#

Review the newly opened results file. The results contain these properties:

script - The name of the script which you ran

run - When you ran the script

elapsed - How long it took the script to complete

status - Whether the script succeeded in executing or errored

result - What was returned by the script. The contents of result are what the script return on completion